#ifndef __PROCESS_POOL_HPP__
#define __PROCESS_POOL_HPP__

#include <iostream>
#include <unistd.h>
#include <vector>
#include <cstdlib>
#include <sys/wait.h>
#include "Task.hpp"

class TaskManager;

class Channel
{
public:
    Channel(int wfd,pid_t fid):_wfd(wfd),_fid(fid) 
    {
        _name="我的父进程id是-"+std::to_string(_fid)+"-"+std::to_string(wfd);
    }

    void send(int taskcode)
    {
        int n=write(_wfd,&taskcode,sizeof(taskcode));
        (void)n;
    }

    void stop()
    {
        close(_wfd);
    }

    void wait()
    {
        pid_t pid = waitpid(_fid,nullptr,0);
        (void)pid;
    }
    int get_wfd(){return _wfd;}
    pid_t get_fid(){return _fid;}
    std::string get_name(){return _name;}

    ~Channel() {}

private:
    int _wfd;
    pid_t _fid;
    std::string _name;
    // int _loadnum;//负载指标
};

class ChannelManage
{
public:
    ChannelManage():_next(0) {}

    void Insert(int pipefd,pid_t pid)
    {
        channels.emplace_back(pipefd,pid);
    }

    Channel& Select()
    {
        Channel& co=channels[_next];
        _next++;
        _next%=channels.size();
        return co;
    }

    void stopprocess()
    {
        for(Channel& c:channels)
        {
            c.stop();
            std::cout<<"关闭了"<<c.get_name()<<std::endl;
        }
    }

    void waitprocess()
    {
        for(Channel& c:channels)
        {
            c.wait();
            std::cout<<"关闭了"<<c.get_name()<<std::endl;
        }
    }
    void PrintChannelInfo()
    {
        for(auto& channel : channels)
        {
            std::cout<<"_wfd:"<<channel.get_wfd()<<"_fid:"<<channel.get_fid()
                     <<"_name:"<<channel.get_name()<<std::endl;
        }
    }

    // int getnext(){return _next;}
    ~ChannelManage() {}

private:
    std::vector<Channel> channels;
    int _next;
};

const int process_num = 5;

class ProcessPool
{
public:
    ProcessPool(int processnum):_processnum(process_num) 
    {
        _tm.Register(PrintLog);
        _tm.Register(DownLoad);
        _tm.Register(Upload);
    }
    ~ProcessPool() {}

    void work(int fd)
    {
        while(true)
        {
            int code=0;
            ssize_t n=read(fd,&code,sizeof(code));
            if(n>0)
            {
                if(n!=sizeof(code))
                {
                    continue;
                }
                std::cout<<"子进程["<<getpid()<<"]收到了一个任务码"<<code<<std::endl;
                _tm.Executable(code);
            }
            else if(n==0)
            {
                //返回0说明写端关闭了，子进程在写，所以子进程推出了
                std::cout<<"子进程退出"<<std::endl;
                break;
            }
            else
            {
                std::cout<<"读取错误"<<std::endl;
                break;
            }
        }

    }

    bool CreateChannel()
    {
        for (int i = 0; i < _processnum; i++)
        {
            // 创建管道
            int fd[2] = {0};
            int n = pipe(fd);

            if (n < 0)
                return false;

            // 创建子进程
            pid_t id = fork();
            if (id < 0)
                return false;
            // 子进程
            else if (id == 0)
            {
                // 关闭读
                close(fd[1]);
                work(fd[0]);
                close(fd[0]);
                exit(1);
            }
            else
            {
                //关闭写
                close(fd[0]);
                _cm.Insert(fd[1],id);
            }
        }
        return true;
        
    }
    void GetInfo()
    {
        _cm.PrintChannelInfo();
    }

    void PushTask()
    {
        //选择进程
        int code=_tm.code();

        Channel& co = _cm.Select();
        std::cout<<"选择了"<<co.get_name()<<"号进程"<<std::endl;

        //做任务
        co.send(code);
        std::cout<<"发送的任务码是"<<code<<std::endl;
    }
    //关闭进程池
    void stop()
    {
        _cm.stopprocess();
        _cm.waitprocess();
    }

private:
    int _processnum;
    ChannelManage _cm;
    TaskManager _tm;
};


#endif